<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- statemachine.cpp -->
  <title>StateMachine QML 类型| Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-qmlmodule.html">QML Types</a></td><td >StateMachine QML 类型</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#properties">属性</a></li>
<li class="level1"><a href="#signals"><span lang="zh-cn">信号</span></a></li>
<li class="level1"><a href="#methods">方法</a></li>
<li class="level1"><a href="#details">详细信息</a></li>
<li class="level2"><a href="#example-usage">示例使用</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">StateMachine QML 类型</h1>
<span class="subtitle"></span>
<!-- $$$StateMachine-brief -->
<p>提供分层的有限状态机。 <a href="#details">更多...</a></p>
<!-- @@@StateMachine -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> 导入语句:</td><td class="memItemRight bottomAlign"> import QtQml.StateMachine 1.12</td></tr><tr><td class="memItemLeft rightAlign topAlign"> 
	<span lang="zh-cn">从：</span></td><td class="memItemRight bottomAlign">  Qt 5.4</td></tr><tr><td class="memItemLeft rightAlign topAlign"> 
		<span lang="zh-cn">继承了：</span></td><td class="memItemRight bottomAlign"> <p><a href="qml-qtqml-statemachine-state.html">State</a></p>
</td></tr></table></div><ul>
<li><a href="qml-qtqml-statemachine-statemachine-members.html"></a></li>
</ul>
<a name="properties"></a>
<h2 id="properties">属性</h2>
<ul>
<li class="fn"><b><b><a href="qml-qtqml-statemachine-statemachine.html#errorString-prop">errorString</a></b></b> : string</li>
<li class="fn"><b><b><a href="qml-qtqml-statemachine-statemachine.html#globalRestorePolicy-prop">globalRestorePolicy</a></b></b> : enumeration</li>
<li class="fn"><b><b><a href="qml-qtqml-statemachine-statemachine.html#running-prop">running</a></b></b> : bool</li>
</ul>
<a name="signals"></a>
<h2 id="signals"><span lang="zh-cn">信号</span></h2>
<ul>
<li class="fn"><b><b><a href="qml-qtqml-statemachine-statemachine.html#started-signal">started</a></b></b>()</li>
<li class="fn"><b><b><a href="qml-qtqml-statemachine-statemachine.html#stopped-signal">stopped</a></b></b>()</li>
</ul>
<a name="methods"></a>
<h2 id="methods">方法</h2>
<ul>
<li class="fn"><b><b><a href="qml-qtqml-statemachine-statemachine.html#start-method">start</a></b></b>()</li>
<li class="fn"><b><b><a href="qml-qtqml-statemachine-statemachine.html#stop-method">stop</a></b></b>()</li>
</ul>
<!-- $$$StateMachine-description -->
<a name="details"></a>
<h2 id="details">详细信息</h2>
<p><a href="qml-qtqml-statemachine-statemachine.html">StateMachine</a>基于Statecharts的概念和表示法。StateMachine是<a href="qmlstatemachine.html">声明式状态机框架</a>的一部分。</p>
<p>状态机管理一组状态和这些状态之间的转换;这些状态和转换定义了状态图。一旦构建了状态图，状态机就可以执行它。<a href="qml-qtqml-statemachine-statemachine.html">StateMachine</a>的执行算法基于<a href="http://www.w3.org/TR/scxml/">状态图XML 
(SCXML)</a>算法。框架概述给出了几个状态图和构建它们的代码。</p>
<p>在启动机器之前，必须设置<a href="qml-qtqml-statemachine-state.html#initialState-prop">initialState</a>。初始状态是机器启动时进入的状态。然后可以将running属性设置为true或在状态机中<a href="qml-qtqml-statemachine-statemachine.html#start-method">start()</a>。启动信号在进入初始状态时发出。</p>
<p>状态机处理事件并进行转换，直到进入顶级最终状态;然后状态机发出finished()信号。您还可以显式地<a href="qml-qtqml-statemachine-statemachine.html#stop-method">stop()</a>状态机(也可以将running属性设置为false)。在这种情况下发出停止信号。</p>
<a name="example-usage"></a>
<h2 id="example-usage">示例使用</h2>
<p>下面的代码片段显示了当单击按钮时将完成的状态机:</p>
<pre class="qml">

  import QtQuick 2.0
  import QtQml.StateMachine 1.0 as DSM

  <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="type"><a href="../qtquickcontrols/qml-qtquick-controls2-button.html">Button</a></span> {
          <span class="name">anchors</span>.fill: <span class="name">parent</span>
          <span class="name">id</span>: <span class="name">button</span>
          <span class="name">text</span>: <span class="string">&quot;Finish state&quot;</span>
          <span class="type">DSM</span>.StateMachine {
              <span class="name">id</span>: <span class="name">stateMachine</span>
              <span class="name">initialState</span>: <span class="name">state</span>
              <span class="name">running</span>: <span class="number">true</span>
              <span class="type">DSM</span>.State {
                  <span class="name">id</span>: <span class="name">state</span>
                  <span class="type">DSM</span>.SignalTransition {
                      <span class="name">targetState</span>: <span class="name">finalState</span>
                      <span class="name">signal</span>: <span class="name">button</span>.<span class="name">clicked</span>
                  }
              }
              <span class="type">DSM</span>.FinalState {
                  <span class="name">id</span>: <span class="name">finalState</span>
              }
              <span class="name">onFinished</span>: <span class="name">Qt</span>.<span class="name">quit</span>()
          }
      }
  }

</pre>
<p>如果遇到错误，机器将查找errorState，如果有可用的<a href="qml-qtqml-statemachine-state.html#errorState-prop">errorState</a>，它将进入该状态。进入错误状态后，可以使用error()检索错误的类型。当进入错误状态时，状态图的执行不会停止。如果没有错误状态应用于错误状态，则机器将停止执行，并将一条错误消息打印到控制台。</p>
<br style="clear: both" /><p><b>请参考 </b><a href="qml-qtqml-statemachine-qabstractstate.html">QAbstractState</a>, <a href="qml-qtqml-statemachine-state.html">State</a>, <a href="qml-qtqml-statemachine-signaltransition.html">SignalTransition</a>, <a href="qml-qtqml-statemachine-timeouttransition.html">TimeoutTransition</a>, <a href="qml-qtqml-statemachine-historystate.html">HistoryState</a>,
	<span lang="zh-cn">和 </span><a href="qmlstatemachine.html">声明式状态机框架</a><span lang="zh-cn">。</span></p>
<!-- @@@StateMachine -->
<h2>属性的文档</h2>
<!-- $$$errorString -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="errorString-prop">
<td class="tblQmlPropNode"><p>
<a name="errorString-prop"></a><span class="qmlreadonly">[read-only] </span><span class="name">errorString</span> : <span class="type"><a href="qml-string.html">string</a></span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>状态机的错误字符串。</p>
</div></div><!-- @@@errorString -->
<br/>
<!-- $$$globalRestorePolicy -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="globalRestorePolicy-prop">
<td class="tblQmlPropNode"><p>
<a name="globalRestorePolicy-prop"></a><span class="name">globalRestorePolicy</span> : <span class="type"><a href="qml-enumeration.html">enumeration</a></span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>此状态机的状态恢复策略。</p>
<p>该属性的默认值是<a href="../qtcore/qstate.html">QState</a>.DontRestoreProperties。</p>
<p>该enum指定恢复策略类型。当机器进入设置一个或多个属性的状态时，恢复策略生效。如果将恢复策略设置为<a href="../qtcore/qstate.html">QState</a><span lang="zh-cn">.</span>RestoreProperties，状态机将在设置新值之前保存属性的原始值。</p>
<p>稍后，当机器进入没有为给定属性设置值的状态时，该属性将自动恢复为初始值。</p>
<p>对于任何给定的属性，只会保存一个初始值。如果状态机已经保存了某个属性的值，那么在成功恢复该属性之前，它不会被覆盖。</p>
<ul>
<li><a href="../qtcore/qstate.html">QState</a>.DontRestoreProperties<span lang="zh-cn">
</span>状态机不应该保存属性的初始值并在以后恢复它们。</li>
<li><a href="../qtcore/qstate.html">QState</a>.RestoreProperties 
状态机应该保存属性的初始值，并在稍后恢复它们。</li>
</ul>
</div></div><!-- @@@globalRestorePolicy -->
<br/>
<!-- $$$running -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="running-prop">
<td class="tblQmlPropNode"><p>
<a name="running-prop"></a><span class="name">running</span> : <span class="type"><a href="qml-bool.html">bool</a></span></p></td></tr>
</table></div>
</div><div class="qmldoc"><p>此状态机的运行状态。</p>
<p><b>请参考 </b><a href="qml-qtqml-statemachine-statemachine.html#start-method">start()</a> 
<span lang="zh-cn">和 </span>
<a href="qml-qtqml-statemachine-statemachine.html#stop-method">stop()</a><span lang="zh-cn">。</span></p>
</div></div><!-- @@@running -->
<br/>
<h2>Signal <span lang="zh-cn">文档</span></h2>
<!-- $$$started -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="started-signal">
<td class="tblQmlFuncNode"><p>
<a name="started-signal"></a><span class="name">started</span>()</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>当状态机进入初始状态(<a href="qml-qtqml-statemachine-state.html#initialState-prop">State::initialState</a>)时发出此信号。</p>
<p>相应的处理程序是<code>onStarted</code>。</p>
<p><b>请参考 </b><a href="qml-qtqml-statemachine-statemachine.html#running-prop">running</a><span lang="zh-cn">，</span> <a href="qml-qtqml-statemachine-statemachine.html#start-method">start()</a><span lang="zh-cn">，</span>
<span lang="zh-cn">和 </span>
<a href="qml-qtqml-statemachine-state.html#finished-signal">State::finished</a><span lang="zh-cn">。</span></p>
</div></div><!-- @@@started -->
<br/>
<!-- $$$stopped -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="stopped-signal">
<td class="tblQmlFuncNode"><p>
<a name="stopped-signal"></a><span class="name">stopped</span>()</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>当状态机停止时发出此信号。</p>
<p>相应的处理程序是<code>onStopped</code>。</p>
<p><b>请参考 </b><a href="qml-qtqml-statemachine-statemachine.html#running-prop">running</a><span lang="zh-cn">，</span> <a href="qml-qtqml-statemachine-statemachine.html#stop-method">stop()</a><span lang="zh-cn">，和 
</span><a href="qml-qtqml-statemachine-state.html#finished-signal">State::finished</a><span lang="zh-cn">。</span></p>
</div></div><!-- @@@stopped -->
<br/>
<h2><span lang="zh-cn">方法文档</span></h2>
<!-- $$$start -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="start-method">
<td class="tblQmlFuncNode"><p>
<a name="start-method"></a><span class="name">start</span>()</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>启动该状态机。机器将重置其配置并转换到初始状态。当输入最终的顶级状态(<a href="qml-qtqml-statemachine-finalstate.html">FinalState</a>)时，机器将发出finished()信号。</p>
<p><b>注意： </b>如果没有正在运行的事件循环，状态机将不会运行，例如主应用程序事件循环是由<a href="../qtcore/qcoreapplication.html#exec">QCoreApplication::exec()</a>()或<a href="../qtwidgets/qapplication.html#exec">QApplication::exec()</a>()启动的。</p><p><b>请参考 </b><a href="qml-qtqml-statemachine-statemachine.html#started-signal">started</a>, <a href="qml-qtqml-statemachine-state.html#finished-signal">State::finished</a>, <a href="qml-qtqml-statemachine-statemachine.html#stop-method">stop()</a>, <a href="qml-qtqml-statemachine-state.html#initialState-prop">State::initialState</a>, and <a href="qml-qtqml-statemachine-statemachine.html#running-prop">running</a>.</p>
</div></div><!-- @@@start -->
<br/>
<!-- $$$stop -->
<div class="qmlitem"><div class="qmlproto">
<div class="table"><table class="qmlname">
<tr valign="top" class="odd" id="stop-method">
<td class="tblQmlFuncNode"><p>
<a name="stop-method"></a><span class="name">stop</span>()</p></td></tr>
</table></div>
</div><div class="qmldoc"><p>停止该状态机。状态机将停止处理事件，然后发出停止信号。</p>
<p><b>请参考 </b><a href="qml-qtqml-statemachine-statemachine.html#stopped-signal">stopped</a><span lang="zh-cn">，</span> <a href="qml-qtqml-statemachine-statemachine.html#start-method">start()</a><span lang="zh-cn">，</span>
<span lang="zh-cn">和 </span>
<a href="qml-qtqml-statemachine-statemachine.html#running-prop">
<span lang="zh-cn">运行</span></a><span lang="zh-cn">。</span></p>
</div></div><!-- @@@stop -->
<br/>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
